package com.rybbaby.tss.core.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @description 服务器监听
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年2月28日 下午5:22:04
 */
public class Log4jListener implements ServletContextListener {
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		log("The server has stopped...");
	}
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		log("The server is started...");
		setLog4jFilePath(sce);
	}
	
	private void setLog4jFilePath(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		String webApp = servletContext.getInitParameter("webAppRootKey");
		String relativeLog4jPath = servletContext.getInitParameter("log4jPath");
		String log4jPath = servletContext.getRealPath(relativeLog4jPath);
		System.setProperty(webApp , log4jPath);
		log("Set the log storage location：" + log4jPath);
	}
	
	private void log(String msg) {
		System.out.println("信息:" + msg);
	}
	
}
